package com.niit.blog.controller;

import com.niit.blog.entity.domain.Blog;
import com.niit.blog.entity.domain.BlogCategory;
import com.niit.blog.service.BlogService;
import com.niit.blog.util.PageQueryUtil;
import com.niit.blog.util.Result;
import com.niit.blog.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class BlogManageController {
    @Autowired
    private BlogService blogService;

    /**
     * @Description: 分类数据页面
     * @Author: NIIT
     * @CreateTime: 2022-5-27 10:16
     * @param: request
     * @return: java.lang.String
     **/
    @GetMapping("/blogs")
    public String blogPage(HttpServletRequest request) {
        request.setAttribute("path", "blogs");
        return "user/blogmanage";
    }

    /**
     * @Description: 查询文章列表
     * @Author: NIIT
     * @CreateTime: 2022-5-27 10:17
     * @param: params
     * @return: com.niit.blog.util.Result
     **/
    @RequestMapping(value = "/blogs/list", method = RequestMethod.GET)
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(blogService.getBlogsPage(pageUtil));
    }

    /**
     * @Description: 新增博客
     * @Author: NIIT
     * @CreateTime: 2022-5-27 10:37
     * @param: categoryName
     * @param: categoryIcon
     * @return: com.niit.blog.util.Result
     **/
    @GetMapping("blogs/edit")
    public String addBlogPage(HttpServletRequest request) {
        request.setAttribute("path", "addBlogs");
        return "user/blogedits";
    }
    @RequestMapping(value = "/blogs/save", method = RequestMethod.POST)
    public String save(Blog blog) {
        String saved = blogService.saveBlog(blog);
        return "user/blogedits";
    }

    /**
     * @Description: 根据ID查询分类详情对象
     * @Author: NIIT
     * @CreateTime: 2022-05-30 03:22:52
     * @param id
     * @return Result
     */
    @GetMapping("/blogs/info/{id}")
    @ResponseBody
    public Result info(@PathVariable("id") Long id) {
        Blog link = blogService.getBlogById(id);
        return ResultGenerator.genSuccessResult(link);
    }

    /**
     * @Description: 博客删除
     * @Author: NIIT
     * @CreateTime: 2022-5-27 12:37
     * @param: ids
     * @return: com.niit.blog.util.Result
     **/
    @RequestMapping(value = "/blogs/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (blogService.deleteBatch(ids)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }
}
